package com.gduf.lsm.service;

import com.gduf.lsm.common.enums.ResultCodeEnum;
import com.gduf.lsm.common.enums.RoleEnum;
import com.gduf.lsm.entity.Account;
import com.gduf.lsm.entity.Older;
import com.gduf.lsm.exception.CustomException;
import com.gduf.lsm.mapper.OlderMapper;
import com.gduf.lsm.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author lsm
 * @Data
 * 老人档案服务层
 */
@Service
public class OlderService {
    @Autowired
    private OlderMapper olderMapper;

    /**
     * 新增
     * @param older
     */
    public void add(Older older) {
        //判断当前用户角色
        Account account=TokenUtils.getCurrentUser();
        String role=account.getRole();
        //若当前为管理员身份则不匹配不可录入
        if(role.equals(RoleEnum.ADMIN.name())){
            throw new CustomException(ResultCodeEnum.USER_NOT_SUITED);
        }
        //添加用户id
        Integer userId= account.getId();
        older.setUserId(userId);
        olderMapper.insert(older);

    }

    /**
     * 删除
     * @param id
     */
    public void deleteById(Integer id) {
        olderMapper.deleteById(id);
    }

    /**
     * 批量删除
     * @param ids
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            olderMapper.deleteById(id);
        }
    }

    /**
     * 修改
     * @param older
     */
    public void updateById(Older older) {
        olderMapper.updateById(older);
    }

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    public Older selectById(Integer id) {
        return olderMapper.selectById(id);
    }

    /**
     * 查询所有
     * @param older
     * @return
     */
    public List<Older> selectAll(Older older) {
        return olderMapper.selectAll(older);
    }

    /**
     * 分页查询
     * @param older
     * @param pageNum
     * @param pageSize
     * @return
     */
    public PageInfo<Older> selectPage(Older older, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Older> list = olderMapper.selectAll(older);
        return PageInfo.of(list);
    }

    /**
     * 查找某用户的老人档案
     * @param userId
     * @return
     */
    public List<Older> selectByUserId(Integer userId) {
        return olderMapper.selectByUserId(userId);
    }

    /**
     * 体检数加一
     * @param oldId
     */
    public void addPhyNum(Integer oldId) {
        olderMapper.addPhyNum(oldId);
    }

    /**
     * 参与活动数加一
     * @param oldId
     */
    public void addCulNum(Integer oldId) {
        olderMapper.addCulNum(oldId);
    }
}
